package com.zhoug.plugin.android.constgenerator.dialog;

import com.zhoug.plugin.android.constgenerator.beans.ConstField;
import com.zhoug.plugin.android.constgenerator.utils.ActionUtils;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.*;
import java.util.List;

public class FieldTableDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTable tableView;

    private OnListener onListener;
    private List<ConstField> mConstFields;


    public FieldTableDialog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        tableView.setRowHeight(30);
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonOK.requestFocus();

                onOK();
                onListener.onOk();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onListener.onCancel();
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }


    public void setConstFields(List<ConstField> constFields) {
        this.mConstFields = constFields;
        DefaultTableModel tableModel = ActionUtils.getTableModel(mConstFields);
        tableView.setModel(tableModel);
        tableView.getModel().addTableModelListener(tableModelListener);
//        tableView.getColumnModel().getColumn(1).setPreferredWidth(20);
    }

    private TableModelListener tableModelListener = new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent event) {
            int row = event.getFirstRow();
            int column = event.getColumn();
            TableModel model = tableView.getModel();
            ConstField constField = mConstFields.get(row);
            String valueAt = (String) model.getValueAt(row, column);
            System.out.println(String.format("row:%d,column:%d,valueAt:%s", row, column, valueAt));
            if(column==0){
                constField.setFieldName(valueAt);
            }else if(column==1){
                constField.setValue(Integer.parseInt(valueAt));
            }else if(column==2){
                constField.setName(valueAt);
            }
        }
    };

    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        FieldTableDialog dialog = new FieldTableDialog();
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    public void setOnListener(OnListener onListener) {
        this.onListener = onListener;
    }

    public interface OnListener {
        void onOk();

        void onCancel();


    }

}
